package org.dydl.dao.system.repository;

import org.dydl.model.system.Production;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


@Repository
public interface IProduceRepository extends CrudRepository<Production, Integer>,JpaSpecificationExecutor<Production>{
	
	@Query(value = "SELECT * FROM production p WHERE p.ORDER_NO = :orderNo" ,nativeQuery = true)
	Production list(@Param("orderNo")String orderNo);
	
	@Query(value = "FROM Production ",countQuery = "SELECT count(id) FROM Production")
	Page<Production> getProduceList(Pageable pageable);
	
	
	@Query(value = " FROM Production  WHERE productionNo LIKE :productionNo", countQuery = "SELECT count(id) FROM Production  WHERE productionNo LIKE :productionNo")
	Page<Production> getProduceListLike(@Param("productionNo") String fullName, Pageable pageable);
	
	@Modifying
	@Transactional
	@Query(value = "UPDATE production set EXAMINATION = 1 WHERE  ID = :id",nativeQuery = true)
	void doExamination(@Param("id")Integer id);
	
	@Modifying
	@Transactional
	@Query(value = "UPDATE production set STATUS = 1 WHERE  ID = :id",nativeQuery = true)
	void complete(@Param("id")Integer id);

	@Modifying
	@Query(value = "UPDATE production set ALREADY = ALREADY + :already WHERE  ORDER_NO = :orderNo",nativeQuery = true)
	void updateAlready(@Param("already")Integer already, @Param("orderNo")String orderNo);
	
	@Modifying
	@Query(value = "UPDATE production set ALREADY_OUT = ALREADY_OUT + :alreadyOut, ALREADY = ALREADY - :alreadyOut WHERE  ORDER_NO = :orderNo",nativeQuery = true)
	void updateAlreadyOut(@Param("alreadyOut")Integer alreadyOut, @Param("orderNo")String orderNo);
	
}